package com.appclient;

import java.net.URL;
import java.util.ArrayList;
import java.util.List;
import java.util.ResourceBundle;
import java.util.Timer;
import javafx.application.Platform;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.Button;
import javafx.scene.layout.GridPane;

/**
 *
 * @author Sawior
 */
public class MenuController implements Initializable, ControlledScreen {

    private ScreensController myController;
    @FXML
    private GridPane grid;
    @FXML
    private Button newClient;
    @FXML
    private Button Others;
    @FXML
    private Button CashLook;
    @FXML
    private Button entry;
    @FXML
    private Button exit;
    @FXML
    private Button newCar;
    @FXML
    private Button tarrifs;
    @FXML
    private Button state;
    public static boolean firstLogin;
    Timer timer;
    LoggedUserPooling timeTask = new LoggedUserPooling(this);
    List<GridButtons> defaultState;
    @FXML
    private Button receivables;
    @FXML
    private Button raports;

    @Override
    public void initialize(URL url, ResourceBundle rb) {

        defaultState = new ArrayList<GridButtons>();
        createDefaultState();
        timer = new Timer(true);
        timer.schedule(timeTask, 1000, 20);
        firstLogin = false;

    }

    @Override
    public void setScreenParent(ScreensController screenParent) {
        myController = screenParent;

    }

    @FXML
    private void goToMain(ActionEvent event) {
        myController.setScreen(ScreensFramework.MAIN_SCREEN);
    }

    @FXML
    private void handleButtonAction(ActionEvent event) {
        if (event.getSource() == newClient) {
            myController.setScreen(ScreensFramework.CLIENT_WIZARD_SCREEN);
        }
        if (event.getSource() == Others) {
            myController.setScreen(ScreensFramework.OTHER_WIZARD_SCREEN);
        }
        if (event.getSource() == raports) {
            myController.setScreen(ScreensFramework.RAPORT_WIZARD_SCREEN);
        }
        if (event.getSource() == entry) {
            myController.setScreen(ScreensFramework.ENTRY_WIZARD_SCREEN);
        }
        if (event.getSource() == exit) {
            myController.setScreen(ScreensFramework.EXIT_WIZARD_SCREEN);
        }
        if (event.getSource() == tarrifs) {
            myController.setScreen(ScreensFramework.TARRIF_SCREEN);
        }
        if (event.getSource() == receivables) {
            myController.setScreen(ScreensFramework.RECEIVABLES_SCREEN);
        }
        if (event.getSource() == newCar) {
            myController.setScreen(ScreensFramework.NEW_CAR_SCREEN);
        }
        if (event.getSource() == state) {
            myController.setScreen(ScreensFramework.STATE_SCREEN);
        }
    }

//Wstawia na miejsce button1 button2
    public void replaceButtons(Button button1, Button button2) {

        int x1 = grid.getColumnIndex(button1);
        int y1 = grid.getRowIndex(button1);
        int x2 = grid.getColumnIndex(button2);
        int y2 = grid.getRowIndex(button2);
        grid.getChildren().remove(button1);
        grid.getChildren().remove(button2);
        grid.add(button2, x1, y1);
        grid.add(button1, x2, y2);

    }

    public void hideButton(Button b) {
        b.setVisible(false);
    }

    public void showButton(Button b) {
        b.setVisible(true);
    }

    public void screenForUser(final String userType) {
        Platform.runLater(new Runnable() {
            @Override
            public void run() {

                if (userType.equals(Role.bookkeeper.getName())) {
                    setScreenForBookkeeper();
                }
                if (userType.equals(Role.admin.getName())) {
                    setScreenForAdmin();
                }
                if (userType.equals(Role.manager.getName())) {
                    setScreenForManager();
                }
                if (userType.equals(Role.porter.getName())) {
                    setScreenForPorter();
                }
            }
        });
    }

    public void setScreenForBookkeeper() {
        if (firstLogin) {
            firstLogin = false;
            restoreDefaultState();
            replaceButtons(newCar, raports);
            replaceButtons(exit, receivables);
        }
        hideButton(newClient);
        hideButton(Others);
        hideButton(entry);
        hideButton(exit);
        hideButton(newCar);
        showButton(receivables);
        hideButton(CashLook);
        showButton(raports);
        showButton(tarrifs);
        hideButton(state);

    }

    public void setScreenForAdmin() {
        //all visible
        if (firstLogin) {
            firstLogin = false;
            restoreDefaultState();
        }
        showButton(newClient);
        showButton(Others);
        showButton(entry);
        showButton(exit);
        showButton(newCar);
        showButton(receivables);
        showButton(raports);
        showButton(tarrifs);
        showButton(state);

    }

    public void setScreenForManager() {
        if (firstLogin) {
            firstLogin = false;
            restoreDefaultState();
        }
        showButton(newClient);
        showButton(Others);
        showButton(entry);
        showButton(exit);
        showButton(newCar);
        showButton(receivables);
        showButton(raports);
        showButton(tarrifs);
        showButton(state);

    }

    public void setScreenForPorter() {
        if (firstLogin) {
            firstLogin = false;
            restoreDefaultState();
        }
        showButton(newClient);
        showButton(Others);
        showButton(entry);
        showButton(exit);
        showButton(newCar);
        showButton(receivables);
        showButton(raports);
        showButton(tarrifs);
        showButton(state);

    }

    public void createDefaultState() {
        defaultState.add(createGridButton(entry, grid));
        defaultState.add(createGridButton(exit, grid));
        defaultState.add(createGridButton(newCar, grid));
        defaultState.add(createGridButton(newClient, grid));
        defaultState.add(createGridButton(raports, grid));
        defaultState.add(createGridButton(tarrifs, grid));
        defaultState.add(createGridButton(state, grid));
        defaultState.add(createGridButton(Others, grid));
        defaultState.add(createGridButton(receivables, grid));
    }

    public GridButtons createGridButton(Button b, GridPane gr) {
    //    System.out.println(b.getText() + " x:" + gr.getColumnIndex(b) + " y:" + gr.getRowIndex(b));
        return new GridButtons((b), gr.getColumnIndex(b), gr.getRowIndex(b));

    }

    public void restoreDefaultState() {
        grid.getChildren().remove(entry);
        grid.getChildren().remove(exit);
        grid.getChildren().remove(state);
        grid.getChildren().remove(tarrifs);
        grid.getChildren().remove(raports);
        grid.getChildren().remove(newCar);
        grid.getChildren().remove(newClient);
        grid.getChildren().remove(Others);
        grid.getChildren().remove(receivables);
        for (GridButtons gr : defaultState) {
            grid.add(gr.getButton(), gr.getX(), gr.getY());
        }
    }
}
